package cn.wolfcode.p2p.business.domain;

import cn.wolfcode.p2p.base.domain.BaseDomain;
import cn.wolfcode.p2p.base.domain.LoginInfo;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;
import org.springframework.format.annotation.DateTimeFormat;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;

import static cn.wolfcode.p2p.util.Constants.*;

/**
 * 借款对象
 */
@Setter
@Getter
public class BidRequest extends BaseDomain {

    private Integer version = 0;

    private Integer returnType = Constants.RETURN_TYPE_MONTH_INTEREST_PRINCIPAL;//还款方式

    private Integer bidRequestType = Constants.BIDREQUEST_TYPE_NORMAL; //借款类型

    private Integer bidRequestState = BIDREQUEST_STATE_PUBLISH_AUDITING; //借款的状态

    private BigDecimal bidRequestAmount; //借款金额

    private BigDecimal currentRate; //借款利率

    private Integer monthes2Return; //借款期限

    private Integer bidCount = 0; //已经有多少次投标

    private BigDecimal totalRewardAmount; //总回报金额

    private BigDecimal currentSum = Constants.ZERO; //已经投了多少钱了

    private String title; //借款标题

    private String description; //借款描述

    private String note; //后台审核备注

    private Date disableDate; //招标到期时间

    private LoginInfo createUser; //借款人

    private Integer disableDays; //招标天数

    private BigDecimal minBidAmount; //投标最小金额

    private List<Bid> bids = new ArrayList<>();

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date applyTime; //申请时间

    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date publishTime; //发布时间

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        BidRequest that = (BidRequest) o;
        return this.id.longValue()==((BidRequest) o).getId().longValue();
    }

    @Override
    public int hashCode() {

        return Objects.hash(id);
    }

    /**
     * 状态名称
     *
     * @return
     */
    public String getBidRequestStateDisplay() {
        switch (this.bidRequestState) {
            case BIDREQUEST_STATE_PUBLISH_AUDITING:
                return "发标待审";
            case BIDREQUEST_STATE_PUBLISH_PENDING:
                return "待发布";
            case BIDREQUEST_STATE_BIDDING:
                return "招标中";
            case BIDREQUEST_STATE_UNDO:
                return "已撤销";
            case BIDREQUEST_STATE_BIDDING_OVERDUE:
                return "流标";
            case BIDREQUEST_STATE_APPROVE_PENDING_1:
                return "满标1审";
            case BIDREQUEST_STATE_APPROVE_PENDING_2:
                return "满标2审";
            case BIDREQUEST_STATE_REJECTED:
                return "满标审核被拒绝";
            case BIDREQUEST_STATE_PAYING_BACK:
                return "还款中";
            case BIDREQUEST_STATE_COMPLETE_PAY_BACK:
                return "已还清";
            case BIDREQUEST_STATE_PAY_BACK_OVERDUE:
                return "逾期";
            case BIDREQUEST_STATE_PUBLISH_REFUSE:
                return "发标审核拒绝状态";
        }
        return "状态异常";
    }

    /**
     * 审核借款json
     *
     * @return
     */
    public String getJsonString() {
        Map<String, Object> map = new HashMap();
        map.put("id", this.id);
        map.put("username", getCreateUser().getUsername());
        map.put("title", this.title);
        map.put("bidRequestAmount", this.bidRequestAmount);
        map.put("currentRate", this.currentRate);
        map.put("monthes2Return", this.monthes2Return);
        map.put("returnType", this.returnType);
        map.put("totalRewardAmount", this.totalRewardAmount);
        try {
            return (new ObjectMapper()).writeValueAsString(map);
        } catch (JsonProcessingException var3) {
            var3.printStackTrace();
            return null;
        }
    }

    /**
     * 还款方式
     */
    public String getReturnTypeDisplay() {
        return this.returnType == RETURN_TYPE_MONTH_INTEREST_PRINCIPAL ? "按月分期" : "按月到期";
    }

    /**
     * 投标进度
     */
    public int getPersent() {
        return this.currentSum
                .divide(this.bidRequestAmount, Constants.CAL_SCALE, RoundingMode.HALF_UP)
                .multiply(CalculatetUtil.ONE_HUNDRED).intValue();
    }

    /**
     * 还需金额
     */
    public BigDecimal getRemainAmount(){
        return bidRequestAmount.subtract(currentSum);
    }

}